hq.export = {
	init: function() {
		console.group('export::init');
		$j('.exportBtn').click(toolbox.delegate(this.exportPolys, this));
		console.groupEnd();

	},

	
        exportPolys: function(evt) {
		console.group('export::exportPolys');

		$j('#exportMsg').hide();

		var $tgt = $j(evt.target);
		var params = toolbox.formVals( $tgt.parents( 'form:first' ) );
		var paramStr = toolbox.queryStr( params );

		var nhs = $j('#exportNhs').find('option');
		var tmp = '';
		var nhNames = [];

		if($tgt.val()=='Export Selected') {

		for(var i=0; i<nhs.length; i++) {
			 tmp = $j(nhs[i]).val();
			if(tmp)
				nhNames.push(tmp);
		}

		if(parseInt(nhNames.length) > 0)	{	
			paramStr += '&exportNhs=' + nhNames.join(',');
		} else {
			alert('Please select at least one neighborhood from the map');
			return false;
		}
		}	
		var script = paramStr.indexOf('=kml')==-1 ? 'export.php' : 'kmlexport.php';

		$j('#exportSpinner').show();

                $j.ajax({
                        url: '/ajax/' + script,
                        dataType: 'json',
			data: paramStr,
                        success: toolbox.delegate(this.exportPolysCB, this)
                });

		console.groupEnd();

		return false;
        },

        exportPolysCB : function(res) {
		console.group('export::exportPolysCB');

		if(res)
			$j('#expDownloadLink').attr('href',  res);

                $j('#exportMsg').show()
                $j('#exportSpinner').hide();

		console.groupEnd();
        },

	
};

$j(document).ready(toolbox.delegate(hq.export.init, hq.export));
